package com.example.boosttestandroid.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.boosttestandroid.R;
import com.example.boosttestandroid.interfac.OnClickTabListener;
import com.example.boosttestandroid.widget.CommonTitleBar;
import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.FlutterBoostRouteOptions;

import java.util.HashMap;
import java.util.Map;

public class HomeFragment extends Fragment implements OnClickTabListener {

    private CommonTitleBar titlebar;
    private TextView btnTV;
    private TextView contentTV;

    private String data = "嗟乎！时运不齐，命途多舛。冯唐易老，李广难封。屈贾谊于长沙，非无圣主；窜梁鸿于海曲，岂乏明时？所赖君子见机，达人知命。老当益壮，宁移白首之心？穷且益坚，不坠青云之志。酌贪泉而觉爽，处涸辙以犹欢。北海虽赊，扶摇可接；东隅已逝，桑榆非晚。孟尝高洁，空余报国之情；阮籍猖狂，岂效穷途之哭！";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View homeView = inflater.inflate(R.layout.fragment_home, container, false);
        initWithHomeFragment(homeView);
        return homeView;
    }

    /**
     * Fragment 初始化
     */
    private void initWithHomeFragment(final View hpView) {
        btnTV = hpView.findViewById(R.id.tv_home_flutter);
        contentTV = hpView.findViewById(R.id.tv_home_content);
        titlebar = hpView.findViewById(R.id.title_view);
        titlebar.setTitleView("首页面 - 这是原生页面");
        titlebar.hideLeftView();

        btnTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("跳转到Flutter");
                gotoFlutter();
            }
        });

        contentTV.setText("怒发冲冠，凭栏处、潇潇雨歇。抬望眼、仰天长啸，壮怀激烈。三十功名尘与土，八千里路云和月。莫等闲、白了少年头，空悲切。\n靖康耻，犹未雪。臣子恨，何时灭。驾长车，踏破贺兰山缺。壮志饥餐胡虏肉，笑谈渴饮匈奴血。待从头、收拾旧山河，朝天阙。");
    }

    /**
     * 跳转到 Flutter 页面
     */
    private void gotoFlutter() {
        Map<String, Object> params = new HashMap();
        params.put("data", data);
        FlutterBoostRouteOptions options = new FlutterBoostRouteOptions.Builder()
                .pageName("mainPage")
                .arguments(params)
                .build();
        FlutterBoost.instance().open(options);
    }

    @Override
    public void onclick(int position) {

    }
}
